package com.yimi.appstore.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.sax.StartElementListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yimi.appstore.R;
import com.yimi.appstore.adapter.AppAdapter;
import com.yimi.appstore.adapter.MyAdapter;
import com.yimi.appstore.util.BitmapUtil;

/**
 * 专题详情页(应用，音乐，视频，小说)
 * 
 * @author Tank
 * 
 */
public class SpecialDetailActivity extends Activity {

	private static final String TAG = "SpecialDetailActivity";
	private Context mContext;
	private MyAdapter mAppAdapter;
	private ListView mAppLv;
	private View mLoadingV;
	private TextView mErrorTv, mTitleTv;
	private ProgressBar mPb;
	private Button mRefreshBtn, mGobackBtn;

	public static void lanuchActiviy(Context cx, String id) {
		Intent i = new Intent(cx, SpecialDetailActivity.class);
		cx.startActivity(i);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.special_detail_layout);
		mContext = this;
		initUi();
		getData();
	}

	private void initUi() {
		mLoadingV = findViewById(R.id.loading_ly);
		mErrorTv = (TextView) findViewById(R.id.tv_error);
		mPb = (ProgressBar) findViewById(R.id.progress_bar);
		mRefreshBtn = (Button) findViewById(R.id.retry);
		mAppLv = (ListView) findViewById(R.id.special_detail_lv);
		mAppAdapter = new AppAdapter(mContext,
				new ArrayList<HashMap<String, Object>>());
		
		mGobackBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	private void getData() {

		new AsyncTask<Void, Void, List<HashMap<String, Object>>>() {

			protected void onPreExecute() {

				// 加载数据时，先销毁掉异步加载图片任务。
				BitmapUtil.destroyedAsyncTask();
				mLoadingV.setVisibility(View.VISIBLE);
				mRefreshBtn.setVisibility(View.GONE);
				mErrorTv.setText("正在加载……");
				mPb.setVisibility(View.VISIBLE);

			};

			@Override
			protected List<HashMap<String, Object>> doInBackground(
					Void... params) {
				List<HashMap<String, Object>> result = new ArrayList<HashMap<String, Object>>();
				result = new ArrayList<HashMap<String, Object>>();
				for (int i = 0; i < 10; i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					result.add(map);
				}
				return result;
			}

			protected void onPostExecute(List<HashMap<String, Object>> result) {
				mPb.setVisibility(View.GONE);
				if (result != null && !result.isEmpty()) {
					mLoadingV.setVisibility(View.GONE);
					mRefreshBtn.setVisibility(View.GONE);
					mErrorTv.setVisibility(View.GONE);
					mAppLv.setVisibility(View.VISIBLE);
					mAppAdapter.updateListData(result);
					mAppLv.setAdapter(mAppAdapter);
					// 异步加载图片任务
					// updateBitmap(id);
				} else {
					mRefreshBtn.setVisibility(View.VISIBLE);
					mRefreshBtn.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							getData();
						}
					});
					mErrorTv.setText("加载数据失败，请重试");
					mErrorTv.setVisibility(View.VISIBLE);
				}
			};
		}.execute();

	}

}
